package ru.niir.goodfil.audit.util;

import com.thoughtworks.xstream.XStream;
import org.apache.myfaces.shared_tomahawk.util.Assert;

import java.util.List;

public class XMLUtils {
    public static String getObjectXml(Object o) {
        Assert.notNull(o);

        StringablePropertiesList object = EntityUtils.getStringablePropertiesList(o);
        XStream xStream = new XStream();
        String objectXml = xStream.toXML(object);
        return objectXml;
    }

    public static StringablePropertiesList getObjectXmlFromString(String s) {
        XStream xStream = new XStream();
        return (StringablePropertiesList) xStream.fromXML(s);
    }

    public static String getObjectChangeXml(Object o1, Object o2) {
        List<PropertyHistory> propertyHistories = EntityUtils.compareEntities(o1, o2);
        PropertyHistoryPrinter printer = new PropertyHistoryPrinter();
        StringablePropertyHistoriesList object = printer.getStringablePropertyHistoriesList(propertyHistories);
        XStream xStream = new XStream();
        String objectXml = xStream.toXML(object);
        return objectXml;
    }

    public static StringablePropertyHistoriesList getObjectChangeXmlFromString(String s) {
        XStream xStream = new XStream();
        return (StringablePropertyHistoriesList) xStream.fromXML(s);
    }
}
